// LEDMatrixDisplay.cpp
#include "LEDMatrixDisplay.h"

LEDMatrixDisplay::LEDMatrixDisplay(uint8_t pin, uint16_t width, uint16_t height) 
    : LED_WIDTH(width), LED_HEIGHT(height), NUM_LEDS(width * height), layout(width, height) {
    leds = new CRGB[NUM_LEDS];
    // 使用传入的引脚参数
    FastLED.addLeds<WS2812, PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
}

LEDMatrixDisplay::~LEDMatrixDisplay() {
    delete[] leds;
}

void LEDMatrixDisplay::begin() {
    FastLED.clear();
    FastLED.show();
}

void LEDMatrixDisplay::clear() { 
    FastLED.clear(); 
}

void LEDMatrixDisplay::show() { 
    FastLED.show(); 
}

void LEDMatrixDisplay::setBrightness(uint8_t brightness) { 
    FastLED.setBrightness(brightness); 
}

void LEDMatrixDisplay::setTimeColor(CRGB hourColor, CRGB minColor, CRGB secColor) {
    this->hourColor = hourColor;
    this->minColor = minColor;
    this->secColor = secColor;
}

void LEDMatrixDisplay::setColonColor(CRGB colonColor) {
    this->colonColor = colonColor;
}

void LEDMatrixDisplay::setDateColor(CRGB dateColor) {
    this->dateColor = dateColor;
}

void LEDMatrixDisplay::animateColons() {
    unsigned long currentMillis = millis();
    if (currentMillis - lastColonBlink > colonBlinkInterval) {
        colonState = !colonState;
        lastColonBlink = currentMillis;
    }
}

void LEDMatrixDisplay::setWeekColors(CRGB activeColor, CRGB inactiveColor) {
    this->weekActiveColor = activeColor;
    this->weekInactiveColor = inactiveColor;
}

void LEDMatrixDisplay::displayTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint8_t dayOfWeek) {
    clear();
    animateColons();
    
    uint8_t timeX = layout.getTimePositionX();
    uint8_t timeY = layout.getTimePositionY();
    
    // 小时显示
    layout.drawDigit(leds, timeX, timeY, hours / 10, hourColor);
    layout.drawDigit(leds, timeX + 4, timeY, hours % 10, hourColor);
    
    // 第一个冒号
    if (colonState) {
        layout.drawColon(leds, timeX + 8, timeY, colonColor);
    }
    
    // 分钟显示
    layout.drawDigit(leds, timeX + 10, timeY, minutes / 10, minColor);
    layout.drawDigit(leds, timeX + 14, timeY, minutes % 10, minColor);
    
    // 第二个冒号
    if (colonState) {
        layout.drawColon(leds, timeX + 18, timeY, colonColor);
    }
    
    // 秒显示
    layout.drawDigit(leds, timeX + 20, timeY, seconds / 10, secColor);
    layout.drawDigit(leds, timeX + 24, timeY, seconds % 10, secColor);
    
    // 在底部绘制星期指示器
    layout.drawWeekIndicator(leds, dayOfWeek, weekActiveColor, weekInactiveColor);
    
    // 立即显示
    FastLED.show();
}

void LEDMatrixDisplay::drawDate(uint8_t month, uint8_t day, uint8_t dayOfWeek, CRGB color) {
    clear();
    uint8_t dateX = layout.getDatePositionX();
    uint8_t dateY = layout.getDatePositionY();
    
    // 绘制日历图标
    layout.drawCalendarIcon(leds, dateX, dateY, color);
    
    // 月份十位
    layout.drawSmallDigit(leds, dateX + 5, dateY, month / 10, color);
    layout.drawSmallDigit(leds, dateX + 9, dateY, month % 10, color);
    
    // 分隔符
    int sepX = dateX + 13;
    if (sepX < layout.getWidth() && dateY + 2 < layout.getHeight()) {
        leds[layout.getLedIndex(sepX, dateY + 2)] = color;
        if (sepX + 1 < layout.getWidth()) {
            leds[layout.getLedIndex(sepX + 1, dateY + 2)] = color;
        }
    }
    
    // 日期十位
    layout.drawSmallDigit(leds, dateX + 16, dateY, day / 10, color);
    layout.drawSmallDigit(leds, dateX + 20, dateY, day % 10, color);
    
    // 在底部绘制星期指示器
    layout.drawWeekIndicator(leds, dayOfWeek, weekActiveColor, weekInactiveColor);
    
    FastLED.show();
}

void LEDMatrixDisplay::displayError() {
    static uint8_t hue = 0;
    static uint8_t offset = 0;
    
    clear();
    
    // 创建移动的彩虹效果
    for (uint8_t x = 0; x < LED_WIDTH; x++) {
        for (uint8_t y = 0; y < LED_HEIGHT; y++) {
            uint8_t pixelHue = hue + (x * 10) + (y * 10) + offset;
            int index = layout.getLedIndex(x, y);
            if (index >= 0 && index < NUM_LEDS) {
                leds[index] = CHSV(pixelHue, 255, 255);
            }
        }
    }
    
    hue += 2;
    offset += 5;
    FastLED.show();
}

void LEDMatrixDisplay::fillScreen(CRGB color) {
    fill_solid(leds, NUM_LEDS, color);
    FastLED.show();
}

void LEDMatrixDisplay::testDrawDigit(uint8_t x, uint8_t y, uint8_t num, CRGB color) {
    layout.drawDigit(leds, x, y, num, color);
    FastLED.show();
}